--血战麻将机器人逻辑
package.path  = "../fishnode/?.lua;" .. package.path

local base = require "base"
local skynet = require "skynet"
local tabletool = require "tabletool"
local timetool = require "timetool"

local baseLogic = require "robotlogic/baselogic"

local RobotLogic = baseLogic:new()

--实际的处理逻辑
local Handler = { }

local gamedata = {}
local bullet_id = 10000

-----------------------
--需要重写的方法

--[Comment]
--进桌
--
function RobotLogic:entertableR(msghead, msgbody)
    self.service.table_type = msgbody.gameinfo.room_type
    gamedata.game_type = msgbody.gameinfo.game_type
    gamedata.cur_player_num = msgbody.gameinfo.cur_player_num
    --调用父类实现
    self:super(msghead, msgbody)
end

--[Comment]
--重进桌
--
function RobotLogic:reentertableR(msghead, msgbody)
    self.service.table_type = msgbody.gameinfo.room_type
    --调用父类实现
    self:super(msghead, msgbody)
end

------------------
local function shoot(self)
    if not gamedata.seat_index then return end
    if self.service.state == 2 then
        local angle = math.random(-75, 75)
        bullet_id = bullet_id + 1
        if self.service.doaction then
            self.service:doaction({
                angle = angle,
                id = bullet_id,
            })
        end
    end
    skynet.sleep(50, 60)
    if gamedata.diamond > 0 then
        shoot(self)
    end
end

function RobotLogic:sitdowntableN(msghead, msgbody)
    gamedata.cur_player_num = gamedata.cur_player_num + 1
    if msgbody.rid ~= self.service.rid then 
        return
    end
    -- 初始化桌子数据
    gamedata.seat_index = msgbody.seat.index
    gamedata.diamond =  msgbody.seat.diamond
    self:super(msghead, msgbody)
    skynet.sleep(200)
    if gamedata.diamond > 0 then
        skynet.fork(function()
            pcall(shoot, self)
        end)
    end
end

function RobotLogic:moneychangeN( msghead, msgbody )
    for _, v in ipairs(msgbody.money_change_list or {}) do
        if v.rid == self.service.rid then
            gamedata.diamond = msgbody.money_change_list[1].final_value
            break
        end
    end
end

function RobotLogic:leavetableN(msghead, msgbody)
    if msgbody.rid ~= self.service.rid then 
        return
    end
    gamedata.seat_index = nil
    self:super(msghead, msgbody, "leavetableN")
end

function RobotLogic:standuptableN(msghead, msgbody)
    gamedata.cur_player_num = gamedata.cur_player_num - 1
    if gamedata.cur_player_num <= 1 then
        gamedata.seat_index = nil
    end
    if msgbody.rid ~= self.service.rid then 
        return
    end
    gamedata.seat_index = nil
    self:super(msghead, msgbody, "standuptableN")
    self:leavetableN(msghead, msgbody)
end

return RobotLogic
